package com.sanpu.kxe.business.main.presenter;

import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.widget.LinearLayout;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.RebateTotalOutput;
import com.sanpu.kxe.business.main.contract.ProfitsContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.ScreenUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：收益
 * Created by 薛勇军 on 2018/8/7.
 */

public class ProfitsPresenter implements ProfitsContract.Presenter {

    private ProfitsContract.View mView;

    private CompositeSubscription mSubscription;

    private RoundTextView mIndicatorView[];

    public ProfitsPresenter(ProfitsContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void indicatorView() {
        mIndicatorView = new RoundTextView[3];
        int viewWidth, color;
        for (int i = 0; i < 3; i++) {
            mIndicatorView[i] = new RoundTextView(mView.getContext());
            if (i == 0) {
                viewWidth = ScreenUtil.dp2px(7);
                color = ContextCompat.getColor(mView.getContext(), R.color.color_base);
            } else {
                viewWidth = ScreenUtil.dp2px(5);
                color = ContextCompat.getColor(mView.getContext(), R.color.c_cccccc);
            }
            mIndicatorView[i].setWidth(viewWidth);
            mIndicatorView[i].setHeight(viewWidth);
            mIndicatorView[i].setGravity(Gravity.CENTER);
            mIndicatorView[i].getDelegate();
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, ScreenUtil.dp2px(5), 0);
            mIndicatorView[i].setLayoutParams(params);
            mIndicatorView[i].getDelegate().setBackgroundColor(color);
            mView.addIndicatorView(mIndicatorView[i]);
        }
    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < mIndicatorView.length; i++) {
            mIndicatorView[i].getDelegate().setBackgroundColor(
                    ContextCompat.getColor(mView.getContext(), R.color.c_cccccc));
            mIndicatorView[i].setWidth(ScreenUtil.dp2px(5));
            mIndicatorView[i].setHeight(ScreenUtil.dp2px(5));
        }
        int color = ContextCompat.getColor(mView.getContext(), R.color.color_base);
        mIndicatorView[position].getDelegate().setBackgroundColor(color);
        mIndicatorView[position].setWidth(ScreenUtil.dp2px(7));
        mIndicatorView[position].setHeight(ScreenUtil.dp2px(7));
    }

    @Override
    public void getRebateTotal() {
        Subscription subscription = MainModel.getInstance().rebateTotal()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RebateTotalOutput>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showRebateTotalView(null);
                    }

                    @Override
                    public void onNext(RebateTotalOutput output) {
                        if (output != null) {
                            CacheManage.getInstance().saveCache(CacheModel.REBATETOTAL, output);
                        }
                        mView.showRebateTotalView(output);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
